//
//  M_PaiHangViewController.m
//  Model
//
//  Created by apple on 2018/7/9.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "M_PaiHangViewController.h"
#import "M_PaiHangOneTableViewCell.h"
#import "M_PaiHangTwoTableViewCell.h"
#import "M_PhotoMineGRViewController.h"

#import "MJRefresh.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"
#import "M_MineHelpZXViewController.h"//客服
@interface M_PaiHangViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *hotArr3;
@property (nonatomic, strong) NSMutableArray *hotArr4;
@property (nonatomic, strong) UIView *rootView;
@property (nonatomic, assign) NSInteger page;
@end

@implementation M_PaiHangViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    __weak typeof(self) weakself = self;
    [self creationData];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself creationDataJZ];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself creationData];
        
        [self.tableView.mj_header beginRefreshing];
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    self.title = @"排行榜";
    self.rootView = [[UIView alloc] init];
    [self.view addSubview:self.rootView];
    [self.rootView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-(KIsiPhoneX?84:49));
        
    }];
    self.rootView.backgroundColor = getColor(@"fe45cf");
    
    [self creationView];
    [self regeditCell];
    
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img1 = [UIImage imageNamed:@"b_kefu"];
    leftBtn.frame=CGRectMake(0, 0, img1.size.width, img1.size.height);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"b_kefu"] forState:UIControlStateNormal];
    
    
    [leftBtn addTarget:self action:@selector(keFuClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarBtn=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftBarBtn;
    
    
    // Do any additional setup after loading the view.
}

- (void)keFuClick{
    M_MineHelpZXViewController *vc = [[M_MineHelpZXViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

//- (NSMutableArray *)dataArr{
//    if (!_dataArr) {
//        _dataArr = [NSMutableArray array];
//    }
//    return _dataArr;
//}
- (void)creationView{
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49-(KIsiPhoneX?55:0));
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = getColor(@"fe45cf");
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.rootView addSubview:self.tableView];
    
}
//- (NSMutableArray *)dataArr{
//    if (!_dataArr) {
//        _dataArr = [NSMutableArray array];
//    }
//    return _dataArr;
//}
//-(NSMutableArray *)hotArr3{
//
//    if (!_hotArr3) {
//        _hotArr3 = [NSMutableArray array];
//
//    }
//    return _hotArr3;
//
//}
//- (NSMutableArray *)hotArr4{
//
//    if (!_hotArr4) {
//        _hotArr4 = [NSMutableArray array];
//    }
//    return _hotArr4;
//
//}
#pragma mark ---------  搜索结果-----------
- (void)creationData{
    _dataArr = [NSMutableArray array];
    self.hotArr3 = [NSMutableArray array];
    self.hotArr4 = [NSMutableArray array];
    [self.tableView.mj_header endRefreshing];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    
    hud.tag = 500;
        //提示文本

    hud.labelText = @"正在加载...";
        //是否有遮罩
    
    hud.dimBackground = YES;
    
    _page = 1;
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    [productParams setValue:strPage forKey:@"page"];

//    [productParams setValue:@"10" forKey:@"pagesize"];
    [[MNetworkHandler sharedInstance]conURL:selhotmodel networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"Status"]intValue]==200) {
            
            self.dataArr = [returnData objectForKey:@"Data"];
            
            if (self.dataArr.count > 3) {
                
                for (int i = 0; i < 3; i++) {
                    
                    [self.hotArr3 addObject:self.dataArr[i]];
                    
                }
                for (int i = 3; i < self.dataArr.count; i++) {
                    
                    [self.hotArr4 addObject:self.dataArr[i]];
                    
                }
                
            }else{
                for (int i = 0; i < self.dataArr.count; i++) {
                    
                    [self.hotArr3 addObject:self.dataArr[i]];
                    
                }   
            }

            NSLog(@"DDDDDDDDDDDD%@", self.hotArr3);
            
            [self.tableView reloadData];
            
            [hud removeFromSuperview];
        }
        
        else {
            
//            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                        [hud removeFromSuperview];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                [hud removeFromSuperview];
    }];
    
}
- (void)creationDataJZ{
    
    _page++;
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    [productParams setValue:strPage forKey:@"page"];

//    [productParams setValue:@"10" forKey:@"pagesize"];
    
    
    [[MNetworkHandler sharedInstance]conURL:selhotmodel networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"Status"]intValue]==200) {
            
            NSMutableArray *arr =[returnData objectForKey:@"Data"];
            
            if (arr.count == 0) {
                
                [self.tableView.mj_footer endRefreshing];
                
            }else {
                
                [self.hotArr4 addObjectsFromArray:arr];
                
                NSLog(@"JZJZJZJZJZJJZJZJZJZJZJJZJZ%@", self.dataArr);
                
                
            }
            //
            //
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            //
        }
        //
        else {
            //
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"Msg"] duration:5.0f];
            [self.tableView.mj_footer endRefreshing];
            //
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
}
#pragma mark ------------

- (void)regeditCell{

    [self.tableView registerNib:[UINib nibWithNibName:@"M_PaiHangOneTableViewCell" bundle:nil] forCellReuseIdentifier:@"M_PaiHangOneTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"M_PaiHangTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"M_PaiHangTwoTableViewCell"];
}
#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 0;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.hotArr3.count;
    }else{
        if (self.dataArr.count > 3) {
           return self.dataArr.count-3;
        }else{
            return 0;
        }
        
    }
 
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 132;
    }else{
        
        return 72;
    }
    
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {

        M_PaiHangOneTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"M_PaiHangOneTableViewCell" forIndexPath:indexPath];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.hotArr3.count == 0) {
            
        }else{
            NSMutableDictionary *dic = self.hotArr3[indexPath.row];
            cell.name.text = [dic objectForKey:@"name"];
            
            NSString *strUrl = [[dic objectForKey:@"bwh"] stringByReplacingOccurrencesOfString:@"," withString:@"/"];
            
            cell.bwh.text = strUrl;
            
            [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"head"]] placeholderImage:[UIImage imageNamed:@""]];
            cell.height.text = [NSString stringWithFormat:@"%ldCM",[[dic objectForKey:@"height"]integerValue]];
            cell.hot.text = [NSString stringWithFormat:@"%ld",[[dic objectForKey:@"hot"]integerValue]];
            cell.weight.text = [NSString stringWithFormat:@"%ldKG",[[dic objectForKey:@"weight"]integerValue]];
            cell.num1.text = [NSString stringWithFormat:@"NO.%ld", indexPath.row+1];
            
            [cell.imgView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"BackImg"]] placeholderImage:[UIImage imageNamed:@""]];
            
        }
        
        
        cell.imgView.contentMode = UIViewContentModeScaleAspectFill;
        cell.imgView.clipsToBounds = YES;
        cell.imgView.userInteractionEnabled = YES;

        return cell;
        
    }else{
        M_PaiHangTwoTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"M_PaiHangTwoTableViewCell" forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.dataArr.count == 0) {
            
        }else{
            NSMutableDictionary *dic = self.dataArr[indexPath.row + 3];
            cell.num1.text = [NSString stringWithFormat:@"NO.%ld", indexPath.row+4];
            cell.hot.text = [NSString stringWithFormat:@"%ld",[[dic objectForKey:@"hot"]integerValue]];
            [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"head"]] placeholderImage:[UIImage imageNamed:@""]];
            cell.name.text = [dic objectForKey:@"name"];
            
        }
        
        return cell;
        
    }
    
    
    
}

#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_PhotoMineGRViewController *vc = [[M_PhotoMineGRViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    if (indexPath.section == 0) {
        vc.name = [self.hotArr3[indexPath.row] objectForKey:@"name"];
        vc.headImg = [self.hotArr3[indexPath.row] objectForKey:@"head"];
        vc.modelID = [[self.hotArr3[indexPath.row] objectForKey:@"id"] integerValue];
    }else{
        vc.name = [self.dataArr[indexPath.row+3] objectForKey:@"name"];
        vc.modelID = [[self.dataArr[indexPath.row+3] objectForKey:@"id"] integerValue];
        vc.headImg = [self.dataArr[indexPath.row] objectForKey:@"head"];
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
